/*
 * IJA2011, Board game "Clovece nezlob se"
 * grp108
 * Tomáš Mičiak,xmicia00
 * Andrej Martinák, xmarti51
 *
 */


package src.clovece.game;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class ServerData{

    private static ServerData instance = new ServerData();
    private Socket connections [] = new Socket[4];
    private int index = 0;
    private OutputStreamWriter os [] = new OutputStreamWriter[4];
    private BufferedWriter out  [] =  new BufferedWriter[4];
    private String playerNames [] = new String [4];
    private boolean NEWGAME = false;
    private int numberOfPlayers = 2;
    private int gameBoardSize = 36;
    private String player [] = new String[4];


    private ServerData() {
        super();
    }

    public static ServerData getServerDataInstance() {
        return ServerData.instance;
    }


    public void addConnection(Socket connection){ 

        this.connections[index] = connection;
        try{
            this.os[this.index] = new OutputStreamWriter(connection.getOutputStream());
            this.out[this.index] = new BufferedWriter(os[this.index]);
        }
        catch (IOException f) {
          System.out.println("IOException: " + f);
        }
          catch (Exception g) {
          System.out.println("Exception: " + g);
        }

        this.index++;

    }

    public int getIndex()
    {
        return this.index;
    }

    public Socket getSocket(int i)
    {
        return this.connections[i];
    }

    public void userDisconnected()
    {
        this.connections[index] = null;
        if (this.index != 0)
                this.index--;
    }


    public BufferedWriter getOutputStream(int i)
    {
         return out[i];
    }

    public void setNextPlayerName(String login)
    {
        playerNames[this.index] = login;

    }


    public void setNewgame(boolean x)
    {
        this.NEWGAME = x;
    }

    public boolean getNewgame()
    {
        return this.NEWGAME;
    }


    public void setNumberOfPlayers(int number){
        this.numberOfPlayers = number;
    }

    public int getNumberOfPlayers()
    {
        return this.numberOfPlayers;
    }

    public void setGameBoardSize(int size)
    {
        this.gameBoardSize = size;
    }

    public int getGameBoardSize()
    {
        return this.gameBoardSize;
    }

    public void setPlayerName (String name)
    {
        this.player[this.index-1] = name;

    }

    public String getPlayerName(int i)
    {
        return this.player[i];
    }

    public int getSocketIndex(Socket socket)
    {
        int i = 0;
        while (socket != this.connections[i])
        {
            i++;
        }
        return i;
    }

}





